package com.brilliance.project.service.impl.sysmanage;

import java.util.List;

import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.brilliance.project.common.tools.MD5Util;
import com.brilliance.project.dao.IAgentDao;
import com.brilliance.project.service.impl.BaseService;
import com.brilliance.project.webapp.form.BaseForm;
import com.brilliance.project.webapp.form.sysmanage.AgentForm;

@Component("agentServiceImpl")
public class AgentServiceImpl extends BaseService {
	@Autowired
	private IAgentDao agentDaoImpl;

	@Override
	public List<BaseForm> queryImpl(BaseForm baseForm, Log log)
			throws Exception {
		AgentForm agentForm = (AgentForm) baseForm;
		return agentDaoImpl.query(agentForm);
	}

	@Override
	public List<BaseForm> queryByPgImpl(BaseForm baseForm, Log log) {
		AgentForm agentForm = (AgentForm) baseForm;
		return agentDaoImpl.queryByPg(agentForm);
	}

	@Override
	public boolean insertImpl(BaseForm baseForm, Log log) {
		AgentForm agentForm = (AgentForm) baseForm;
		// 对密码做MD5加密
		agentForm.setAgent_pwd(MD5Util.ecodeByMD5(agentForm.getAgent_pwd()));
		return agentDaoImpl.insert(agentForm);
	}

	@Override
	public boolean deleteImpl(BaseForm baseForm, Log log) throws Exception {
		AgentForm agentForm = (AgentForm) baseForm;
		String[] agentIds = agentForm.getAgent_id().split(",");
		for (String s : agentIds) {
			if (!agentDaoImpl.delete(s)) {
				throw new Exception("删除失败。");
			}
		}
		return true;
	}

	@Override
	public boolean updateImpl(BaseForm baseForm, Log log) {
		AgentForm agentForm = (AgentForm) baseForm;
		return agentDaoImpl.update(agentForm);
	}

}
